﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple.Service;
using Simple.Web.Framework.Infrastructure.Aop;
using Simple.ViewModel;
using Simple.Common.Pager;
using Simple.Common.EasyUICommon;
using Simple.Web.Framework.Infrastructure;
using Simple.Common;
using Simple.Entity.Enums;

namespace HBootStrap.Admin.Controllers
{
    public class RoleController : BaseController
    {
        private readonly RoleService _roleService;
        public RoleController(RoleService roleService)
        {
            _roleService = roleService;
        }

        [MenuViewFilter(MenuName = "角色管理", ModularName = "基础信息管理", Sort = 2)]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AjaxIndex(SimpleMvcPage mvcPage, RoleQueryModel queryModel)
        {
            var model = _roleService.GetRoles(mvcPage, queryModel);
            return Json(new PageDataGrid { rows = model, total = model.TotalCount }, JsonRequestBehavior.AllowGet);
        }

        [ActionViewFilter(ActionName = "添加角色", MenuName = "角色管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加角色", MenuName = "角色管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        public ActionResult Add(RoleDto dto)
        {
            var result = _roleService.Add(dto.ToEntity());
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Role");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "编辑角色", MenuName = "角色管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(long id)
        {
            var model = _roleService.FindById(id);
            var dto = model.ToDto();
            return View(dto);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "编辑角色", MenuName = "角色管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(RoleDto dto)
        {
            var updateRole = _roleService.FindById(dto.Id);
            updateRole.RoleName = dto.RoleName;
            var result = _roleService.Update(updateRole);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Role");
            }
            return Json(result);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "删除角色", MenuName = "角色管理", Sort = 3, ActionType = ActionTypeEnum.Delete)]
        public ActionResult Delete(long id)
        {
            var result = _roleService.Delete(id);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Role");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "分配权限", MenuName = "角色管理", Sort = 4, ActionType = ActionTypeEnum.Set)]
        public ActionResult DistributionAction(long id)
        {
            ViewData["RoleId"] = id;
            return View();
        }

        public ActionResult AjaxDistributionAction(long id)
        {
            var model = _roleService.GetMenuTree(id);
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "分配权限", MenuName = "角色管理", Sort = 4, ActionType = ActionTypeEnum.Set)]
        public ActionResult DistributionAction(RoleDistributionActionDto dto)
        {
            var result = _roleService.DistributionAction(dto.RoleId, dto.MenuIdList);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "Role");
            }
            return Json(result);
        }

    }
}
